package com.tengju.bff.interfaces.app.user;

import com.tengju.bff.interfaces.app.user.assembler.UserInfoAssembler;
import com.tengju.bff.interfaces.app.user.vo.UserInfoVO;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.InterfaceException;
import com.tengju.bff.interfaces.shared.SystemException;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.bff.interfaces.shared.servlet.UserServletContext;
import com.tengju.user.application.user.UserInfoService;
import com.tengju.user.application.user.dto.ChangeHeadImageForm;
import com.tengju.user.application.user.dto.ChangeNicknameForm;
import com.tengju.user.domain.model.user.UserInfo;
import com.tengju.user.domain.model.user.UserInfoRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.UnsupportedEncodingException;

@Validated
@RestController
@Api(tags = "用户信息")
@RequestMapping(value = "app/user/")
@Slf4j
public class UserInfoController {


    private UserInfoRepository userInfoRepository;
    private UserInfoService userInfoService;

    @Autowired
    public UserInfoController(UserInfoRepository userInfoRepository, UserInfoService userInfoService) {
        this.userInfoRepository = userInfoRepository;
        this.userInfoService = userInfoService;
    }

    @ApiOperation(value = "DT用户信息", notes = "DT用户信息")
    @RequestMapping(value = "getUserInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<UserInfoVO> getUserInfo() {
        try {
            UserServletContext userServletContext = ServletContextUtil.getServletContext();
            UserInfo userInfo = userInfoRepository.getByUserId(userServletContext.needUserId());
            return ApiResponse.newSuccess(UserInfoAssembler.userToVO(userInfo));
        } catch (InterfaceException e) {
            return ApiResponse.newFailure(e.getCode(), e.getMsg());
        }
    }

    @ApiOperation(value = "修改昵称", notes = "修改昵称")
    @RequestMapping(value = "changeNickname", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> changeNickname(@RequestBody ChangeNicknameForm changeNicknameForm) throws UnsupportedEncodingException {
            UserServletContext userServletContext = ServletContextUtil.getServletContext();
            return ApiResponse.newSuccess(userInfoService.changeNickname(userServletContext.getUserId(),changeNicknameForm.getNewName()));
    }



    @ApiOperation(value = "修改头像", notes = "修改头像")
    @RequestMapping(value = "changeHeadImage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> changeHeadImage(@RequestBody ChangeHeadImageForm changeHeadImageForm) {
        try {
            UserServletContext userServletContext = ServletContextUtil.getServletContext();
            return ApiResponse.newSuccess(userInfoService.changeHeadImage(userServletContext.getUserId(),changeHeadImageForm.getHeadImageUrl()));
        } catch (SystemException e) {
            return ApiResponse.newFailure(e.getErrorCode(), e.getErrorMessage());
        }
    }


    @ApiOperation(value = "头像上传", notes = "头像上传")
    @RequestMapping(value = "uploadHeadImage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<String> uploadHeadImage(@RequestParam("file") MultipartFile multipartFile) {
        try {
            UserServletContext userServletContext = ServletContextUtil.getServletContext();
            return ApiResponse.newSuccess(userInfoService.uploadHeadImage(multipartFile,userServletContext.getUserId()));
        } catch (SystemException e) {
            return ApiResponse.newFailure(e.getErrorCode(), e.getErrorMessage());
        }
    }




}
